package l1j.server.server.model.damage.magic;

import static l1j.server.server.model.skill.L1SkillId.MIND_BREAK;
import l1j.server.server.ActionCodes;
import l1j.server.server.model.L1Character;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1NpcInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.model.damage.L1Dmg;
import l1j.server.server.model.damage.L1MagicOnNpc;
import l1j.server.server.model.damage.L1PcMagic;
import l1j.server.server.model.skill.L1SkillId;
import l1j.server.server.serverpackets.S_DoActionGFX;
import l1j.server.server.serverpackets.S_SystemMessage;
import l1j.server.server.templates.L1Skills;
import l1j.server.server.utils.Random;

public class L1MagicDamage_Pc_Npc extends L1Dmg {

	private L1PcInstance _attacker;

	private L1NpcInstance _target;

	private int _attr;

	public L1MagicDamage_Pc_Npc(L1PcInstance attacker, L1NpcInstance target,
			int attr) {
		_attr = attr;
		_attacker = attacker;
		_target = target;
	}

	public void commitMagic(L1Character target) {
		if (Random.chance100(mr_reduction_chance[target.getMr() + 100])) {
			mul((100 - mr_reduction_percent[target.getMr() + 100]) / 100.0);
			_attacker.sendPackets(new S_SystemMessage("怪物魔防:" + target.getMr()));
			_attacker.sendPackets(new S_SystemMessage("減傷機率:" + mr_reduction_chance[target.getMr()]));
			_attacker.sendPackets(new S_SystemMessage("減傷％數:" + (100 - mr_reduction_percent[target.getMr()]) / 100.0));
		}
		if (_attr == _target.getNpcTemplate().get_weakAttr()) {
			mul(1.5);
		}
	}
	
	// 武器屬性階位傷害
	public void commitAttrByWp(double dmg) {
		// 傷害
		double attrDmg = 0;
		L1ItemInstance weapon = _attacker.getWeapon();
		int weaponAttrKind = weapon.getAttrEnchantKind();
		int weaponAttrLevel = weapon.getAttrEnchantLevel();
		int chance = 1 + (weapon.getEnchantLevel() / 2);
		if (Random.chance100(chance)) {
			if (_target.getNpcTemplate().get_weakAttr() == weaponAttrKind) {
				attrDmg = dmg * 0.1 * weaponAttrLevel;
			} else {
				attrDmg = dmg * 0.05 * weaponAttrLevel;
			}
		}
		add(attrDmg);
		// 輔助能力
		int attrStartTimes = _attacker.getAttrStartTimes();
		if (attrStartTimes > 5) {
			int attrLevel = attrStartTimes / 5;
			switch(weaponAttrKind) {
			case 1:
				;
				break;
			case 2:
				int hpValue = (int)(_attacker.getMaxHp() * 0.01 * attrLevel);
				if (hpValue < 1) {
					hpValue = 1;
				}
				int newHp = _attacker.getCurrentHp() + hpValue;
				_attacker.setCurrentHp(newHp);
				break;
			case 4:
				int mpValue = (int)(_attacker.getMaxMp() * 0.005 * attrLevel);
				if (mpValue < 1) {
					mpValue = 1;
				}
				int newMp = _attacker.getCurrentMp() + mpValue;
				_attacker.setCurrentMp(newMp);
				break;
			case 8:
				if (dmg + getValue() < 80) {
					add(attrLevel);
				} else {
					add((dmg + getValue()) * (0.01 + (0.01 * attrLevel)));
				}
				break;
			}
		}
		if (attrStartTimes <= 15) {
			_attacker.addAttrStartTimes(1);
		}
		if (attrStartTimes == 5 || attrStartTimes == 10 || attrStartTimes == 15) {
			_attacker.sendPackets(new S_SystemMessage("啟動屬性輔助能力第 " + attrStartTimes / 5 + " 層..."));
		}
		if (_attacker.hasSkillEffect(L1SkillId.FIGHTING_STATE)) {
			_attacker.killSkillEffectTimer(L1SkillId.FIGHTING_STATE);
		}
		_attacker.setSkillEffect(L1SkillId.FIGHTING_STATE, 15 * 1000);
	}

	@Override
	public void hurtTarget(boolean actionHurt) {
		commitAttacker(_attacker);
		commitTarget(_target);
		commitMagic(_target);
		_target.receiveDamage(_attacker, getValue());
		if (getValue() > 0 && actionHurt) {
			_target.broadcastPacket(new S_DoActionGFX(_target.getId(),
					ActionCodes.ACTION_Damage));
		}
	}

	@Override
	public void calcDmg() {
		if (_target.isInvincible()) {
			return;
		}
		L1Skills skill = _attacker.getCurrentSkill();
		if (skill == null) {
			zero();
			return;
		}
		if (_attacker.isAttackMiss(_attacker, _target.getNpcTemplate()
				.get_npcId())) {
			zero();
			return;
		}

		_attr = skill.getAttr();

		L1PcMagic pm = new L1PcMagic(_attacker, this);
		pm.calcPcDmg();

		int skillId = _attacker.getCurrentSkill().getId();
		// 心靈破壞
		if (skillId == MIND_BREAK && _target.getCurrentMp() >= 5) {
			_target.setCurrentMp(_target.getCurrentMp() - 5);
			add(_attacker.getWis() * 5);
		}

		L1MagicOnNpc mon = new L1MagicOnNpc(_target, this);
		mon.calcDmgOnNpc();
	}

}
